package cn.edu.controller;

import cn.edu.entity.JournalEntity;
import cn.edu.entity.Operation;
import cn.edu.entity.UserEntity;
import cn.edu.service.JournalService;
import cn.edu.service.TestService;
import cn.edu.service.UserService;
import com.sun.org.glassfish.gmbal.ParameterNames;
import org.springframework.beans.factory.NamedBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Queue;


@Controller
@RequestMapping(value = "journal")
@SessionAttributes({"operations", "user", "journals"})
public class JournalController {
    @Autowired
    private JournalService journalService;


    @RequestMapping(value = "operations/{id}", method = {RequestMethod.GET})
    public String operations(@PathVariable(value = "id") Long id, HttpSession httpSession) {
        List<Operation> operations = journalService.get(id).getOperations();
        UserEntity user = journalService.get(id).getUser();
        httpSession.setAttribute("user",user);
        httpSession.setAttribute("journals",user.getJournalEntities());
        httpSession.setAttribute("operations", operations);
        return "operations";
    }

    @RequestMapping(value = "delete/{id}", method = RequestMethod.GET)
    public String delete(@PathVariable(value = "id") Long id, HttpSession httpSession, SessionStatus
                         sessionStatus) {

        UserEntity user = journalService.get(id).getUser();
        journalService.delete(id);
        httpSession.setAttribute("user",user);
        httpSession.setAttribute("journals",user.getJournalEntities());
        return "logined";

    }
}
